package com.darren.archive.controller;


import com.darren.archive.bean.ResponseBean;
import com.darren.archive.config.AppConfig;
import com.darren.archive.model.SysUser;
import com.darren.archive.service.ISysUserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@Api(value = "系统用户接口",tags = "系统用户接口",description = "系统用户接口")
@RestController
@Slf4j
public class SysUserController {

    @Resource
    private AppConfig appConfig;

    @Resource
    private ISysUserService sysUserService;

    /**
     * 根据用户部门、用户帐号、用户姓名、手机号、邮箱查询用户
     *
     * @param departmentId     用户部门
     * @param userAccount      用户帐号
     * @param userName         用户姓名
     * @param mobile           手机号
     * @param email            邮箱
     * @param pageNum          分页查询的页号
     * @param pageSize         分页查询的页大小
     * @return                 用户信息
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "departmentId",value = "用户部门ID，非必填",paramType = "query",dataType = "Long"),
            @ApiImplicitParam(name = "userAccount",value = "用户帐号，非必填",paramType = "query",dataType = "String"),
            @ApiImplicitParam(name = "userName",value = "用户姓名，非必填",paramType = "query",dataType = "String"),
            @ApiImplicitParam(name = "mobile",value = "手机号，非必填",paramType = "query",dataType = "String"),
            @ApiImplicitParam(name = "email",value = "邮箱,非必填",paramType = "query",dataType = "String"),
            @ApiImplicitParam(name = "pageNum",value = "分页查询的页号,非必填",paramType = "query",dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize",value = "分页查询的页大小,非必填",paramType = "query",dataType = "Integer")
    })
    @ApiOperation(value = "查询用户列表",notes = "根据用户部门、用户帐号、用户姓名、手机号、邮箱查询用户")
    @GetMapping("/sys/user/list")
    public ResponseBean getSysUsers(@RequestParam(name = "departmentId",required = false) Long departmentId,
                                            @RequestParam(name = "userAccount",required = false) String userAccount,
                                            @RequestParam(name = "userName",required = false) String userName,
                                            @RequestParam(name = "mobile",required = false) String mobile,
                                            @RequestParam(name = "email",required = false) String email,
                                            @RequestParam(name = "pageNum",required = false) Integer pageNum,
                                            @RequestParam(name = "pageSize",required = false) Integer pageSize){
        pageNum = (pageNum == null ||  pageNum < 1) ? 1 : pageNum;
        pageSize = (pageSize == null ||  pageSize < 1) ? appConfig.getPageSize() : pageSize;
        PageHelper.startPage(pageNum,pageSize);
        return ResponseBean.isOk(PageInfo.of(sysUserService.getByConditions(departmentId,userAccount,userName,mobile,email)));
    }

    /**
     * 保存系统用户信息，如果 id 为空，则新增；id 不为空，则修改
     * @return  系统用户信息
     */
    @ApiOperation(value = "保存用户",notes = "保存系统用户信息，如果 id 为空，则新增；id 不为空，则修改")
    @PostMapping(value = "/sys/user/save",consumes = "application/json;charset=UTF-8")
    public ResponseBean save(@RequestBody @ApiParam(name="项目人员信息对象",value="传入json格式",required=true) SysUser record){
        record = sysUserService.save(record);
        return ResponseBean.isOk(record);
    }


    /**
     * 根据用户信息id，删除用户信息
     * @param id   用户信息id
     * @return     受影响行数
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "设备费用信息id，必填",paramType = "path",dataType = "Long")
    })
    @ApiOperation(value = "删除用户",notes = "根据用户信息id，删除用户信息，参数ID作为URL的一部分，如：http://localhost/sys/user/id/{id}")
    @DeleteMapping("/sys/user/id/{id}")
    public ResponseBean deleteById( @PathVariable(name = "id") Long id){
        return ResponseBean.isOk(sysUserService.deleteById(id));
    }

    /**
     * 根据用户账号，删除用户信息
     * @param account   用户账号
     * @return     受影响行数
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "account",value = "设备费用信息id，必填",paramType = "path",dataType = "String")
    })
    @ApiOperation(value = "删除用户",notes = "根据用户信息id，删除用户信息，参数ID作为URL的一部分，如：http://localhost/sys/user/account/{account}")
    @DeleteMapping("/sys/user/account/{account}")
    public ResponseBean deleteByAccount( @PathVariable(name = "account") String account){
        return ResponseBean.isOk(sysUserService.deleteByUserAccount(account));
    }

    /**
     * 根据用户信息id，获取用户信息
     * @param id    用户信息id
     * @return      用户信息
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "用户信息id，必填",paramType = "path",dataType = "Long")
    })
    @ApiOperation(value = "主键查询用户信息",notes = "根据用户信息id，获取用户信息，参数ID作为URL的一部分，如：http://localhost/sys/user/id/{id}")
    @GetMapping("/sys/user/id/{id}")
    public ResponseBean getById(@PathVariable(name = "id") Long id){
        return ResponseBean.isOk(sysUserService.getByPrimaryKey(id));
    }

    /**
     * 根据用户账号，获取用户信息
     * @param account    用户账号
     * @return           用户信息
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "account",value = "用户账号，必填",paramType = "path",dataType = "String")
    })
    @ApiOperation(value = "账号查询用户信息",notes = "根据用户账号，获取用户信息，参数account作为URL的一部分，如：http://localhost/sys/user/account/{account}")
    @GetMapping("/sys/user/account/{account}")
    public ResponseBean getByAccount(@PathVariable(name = "account") String account){
        return ResponseBean.isOk(sysUserService.getByUserAccount(account));
    }

}
